package com.weiyin.examination.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.adapter.FlatSharpAdapter;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.listener.DialogResultInterface;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author njf
 * @date 2017/11/24
 */

public class FlatSharpDialog extends Dialog implements FlatSharpAdapter.OnItemClickListener {
    private static final String TAG = FlatSharpDialog.class.getSimpleName();
    private Activity activity;
    private RecyclerView flatSharpDialogRecyclerView;
    private FlatSharpAdapter flatSharpAdapter;
    private boolean type = false;
    String courseTitle;
    private int[] imageId = {R.drawable.flat1, R.drawable.flat2,
            R.drawable.flat3, R.drawable.flat4,
            R.drawable.flat5, R.drawable.flat6,
            R.drawable.flat7, R.drawable.sharp1,
            R.drawable.sharp2, R.drawable.sharp3,
            R.drawable.sharp4, R.drawable.sharp5,
            R.drawable.sharp6, R.drawable.sharp7,
    };

    private int[] timeImageId = {R.drawable.time_signature24, R.drawable.time_signature34,
            R.drawable.time_signature38, R.drawable.time_signature44,
            R.drawable.time_signature68};
    private int[] timeImageId1 = {R.drawable.time_signature24, R.drawable.time_signature34,
            R.drawable.time_signature44,
            R.drawable.time_signature68};

    private List<Integer> imageList;

    private DialogResultInterface dialogResultInterface;

    public void setDialogResultInterface(DialogResultInterface dialogResultInterface) {
        this.dialogResultInterface = dialogResultInterface;
    }

    public FlatSharpDialog(@NonNull Activity context) {
        super(context);
        this.activity = context;
    }

    public FlatSharpDialog(@NonNull Activity context, @StyleRes int themeResId, boolean type) {
        super(context, themeResId);
        this.activity = context;
        this.type = type;
    }

    public FlatSharpDialog(@NonNull Activity context, @StyleRes int themeResId, boolean type, String courseTitle) {
        super(context, themeResId);
        this.activity = context;
        this.type = type;
        this.courseTitle = courseTitle;
    }


    protected FlatSharpDialog(@NonNull Activity context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.activity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flatsharp_dialog_layout);
        LayoutHelper layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(findViewById(R.id.flatSharpDialogLayout));
        imageList = new ArrayList<>();
        setImage();
        initView();
    }

    private void setImage() {
        if (imageList != null) {
            imageList.clear();
            if (!type) {
                for (int anImageId : imageId) {
                    imageList.add(anImageId);
                }
            } else {
                if (StringUtils.isEmpty(courseTitle)) {
                    for (int aTimeImageId : timeImageId) {
                        imageList.add(aTimeImageId);
                    }
                } else {
                    for (int aTimeImageId : timeImageId1) {
                        imageList.add(aTimeImageId);
                    }
                }
            }
        }
    }

    private void initView() {
        flatSharpDialogRecyclerView = (RecyclerView) findViewById(R.id.flatSharpDialogRecyclerView);
        flatSharpDialogRecyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
        LogUtil.i(TAG, "size=" + imageList.size());
        flatSharpAdapter = new FlatSharpAdapter(R.layout.flat_sharp_item, imageList);
        flatSharpDialogRecyclerView.setAdapter(flatSharpAdapter);
        flatSharpAdapter.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(BaseViewHolder holder, int position) {
        if (holder != null) {
            if (dialogResultInterface != null) {
                if (!type) {
                    dialogResultInterface.result(Constant.COURSE_A9_DIALOG_RESULT1, String.valueOf(position), "0");
                } else {
                    dialogResultInterface.result(Constant.COURSE_A9_DIALOG_RESULT2, String.valueOf(position), "0");
                }
            }
            dismiss();
        }
    }
}
